.\SetGlobalVariables.ps1

if ($Debug -eq "true") {
	$DebugPreference = "Continue";
}

.\GenerateBuild.ps1

$WxsWorkFileName = $TempDir + "\__GeneratedInstaller.wxs";
$WxsFileStructureFileName = $TempDir + "\__GeneratedFiles.wxs";

Write-Debug "Building wix <Directory> structure..."
.\Tasks\buildWixDirectories.ps1 $TempBuildDir | set-Content $WxsFileStructureFileName
Write-Debug "Wix <Directory> structure done"

attrib $TempDir\*.* -r -h -s /s
copy-item $InstallerTemplateFileName $WxsWorkFileName
attrib $TempDir\*.* -r -h -s /s

Write-Debug "Appending the generated <Directory> structure..."
.\Tasks\replaceWixDirectoriesInTemplate.ps1 $WxsFileStructureFileName $WxsWorkFileName
Write-Debug "Appending the generated <Directory> structure done"

Write-Debug "Replacing top <Directory> with TARGETDIR..."
.\Tasks\replaceWixTargetDir.ps1 $WxsWorkFileName
Write-Debug "Replacing done"

if ($CustomActionFileName) {
	Write-Debug "Adding c# custom action support..."
	.\Tasks\addCustomActionSupport.ps1 $WxsWorkFileName $CustomActionFileName
	Write-Debug "Adding c# custom action support done"
}

Write-Debug "Generating the componentref/features..."
.\Tasks\generateWixComponentReferences.ps1 $WxsWorkFileName
Write-Debug "Generating the componentref/features done"

Write-Debug "Replacing general parameters like product, etc..."
.\Tasks\replaceWixGeneralParams.ps1 $WxsWorkFileName
Write-Debug "Replacing done"

.\Tools\Wix\candle.exe $WxsWorkFileName -out $TempDir\Temp.wixobj
.\Tools\Wix\light.exe -out $MsiFileName -ext WixUIExtension '-cultures:en-us' $TempDir\Temp.wixobj

if ($Debug -eq "true") {
	$DebugPreference = "SilentlyContinue";
} 

if ($RemoveTempDirOnFinish -eq "true") {
	Remove-Item $TempDir -recurse
}
